package com.joy.web.admin.controller;


import com.joy.common.exception.JsonException;
import com.joy.common.model.ApiResponse;
import com.joy.common.model.vo.JsTreeVO;
import com.joy.web.admin.service.ICommonCdService;
import com.joy.common.constant.CommonCons;
import com.joy.common.enums.Status;
import com.joy.web.admin.entity.CommonCd;
import com.joy.web.base.controller.BaseController;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjzhao
 * @since 2019-09-23
 */
@Controller
@RequestMapping("/admin/common-cd")
public class CommonCdController extends BaseController {

    @Resource
    ICommonCdService commonCdService;

    /**
     * 数据字典管理 初始化页面
     * @return
     */
    @GetMapping(value = {"", "/", "/index" })
    public String index() {
        return "pages/admin/common-cd/index";
    }

    /**
     * 数据字典管理 初始化页面
     * @return
     */
    @PostMapping(value = "/findCommonCdTree")
    @ResponseBody
    public ApiResponse findCommonCdTree() {
        try {
            //获取 数据字典 tree
            List<JsTreeVO> treeList = commonCdService.findAllCommonCdTree();
            return ApiResponse.ofSuccess(treeList);
        } catch (Exception e) {
            throw new JsonException(Status.UNKNOWN_ERROR, "数据字典加载错误");
        }
    }

    /**
     * 数据字典管理 初始化页面
     * @return
     */
    @PostMapping(value = "/findByChildCd/{childCd}")
    @ResponseBody
    public ApiResponse findByChildCd(@PathVariable(value="childCd") String childCd) {
        try {
            //获取 数据字典 tree
            Optional<CommonCd> CommonCdOp = commonCdService.findByChildCd(childCd);
            if (CommonCdOp.isPresent()) {
                return ApiResponse.ofSuccess(CommonCdOp.get());
            } else {
                return ApiResponse.ofStatus(Status.NO_DATA_ERROR, false);
            }

        } catch (Exception e) {
            throw new JsonException(Status.UNKNOWN_ERROR, "数据字典加载错误");
        }
    }

    /**
     * 数据字典管理 初始化页面
     * @return
     */
    @PostMapping(value = "/findListByParentCd/{parentCd}")
    @ResponseBody
    public ApiResponse findListByParentCd(@PathVariable(value="parentCd") String childCd) {
        try {
            //获取 数据字典 tree
            List<CommonCd> commonCdList = commonCdService.findListByParentCd(childCd);
            if (commonCdList != null && commonCdList.size() > 0) {
                return ApiResponse.ofSuccess(commonCdList);
            } else {
                return ApiResponse.ofStatus(Status.NO_DATA_ERROR, false);
            }

        } catch (Exception e) {
            throw new JsonException(Status.UNKNOWN_ERROR, "数据字典加载错误");
        }
    }

    /**
     * 新增
     * @return
     */
    @PostMapping(value = { "/addSave" })
    @ResponseBody
    @CacheEvict(value= CommonCons.MyCache.COMMON, allEntries = true, key = CommonCons.CommonCacheKey.COMMON_CD)
    public ApiResponse addSave(CommonCd commonCd) {
        try {
            //更新用户详情
            boolean upSucc = commonCdService.save(commonCd);
            return ApiResponse.ofSuccess("success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new JsonException(Status.UNKNOWN_ERROR, "新增数据字典错误报错");
        }
    }

    /**
     * 新增
     * @return
     */
    @PostMapping(value = { "/update" })
    @ResponseBody
    public ApiResponse update(CommonCd commonCd) {

        try {
            //更新用户详情
            boolean upSucc = commonCdService.update(commonCd);
            return ApiResponse.ofSuccess("success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new JsonException(Status.UNKNOWN_ERROR, "新增数据字典错误报错");
        }

    }

    /**
     * 新增
     * @return
     */
    @PostMapping(value = { "/delete/{childCd}" })
    @ResponseBody
    public ApiResponse delete(@PathVariable(value="childCd") String childCd) {

        try {
            //更新用户详情
            boolean upSucc = commonCdService.delete(childCd);
            return ApiResponse.ofSuccess("success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new JsonException(Status.UNKNOWN_ERROR, "新增数据字典错误报错");
        }

    }





}
